//magic_forum_index
//艺问艺答首页
exports.magic_forum_index = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    //最终结果
    var resultData = {};

    var limit = restfulParams.limit == undefined ? config.publicConfig.LIMIT : restfulParams.limit;

    var skip = restfulParams.skip == undefined ? 0 : restfulParams.skip;

    var fc_sort = restfulParams.fc_sort;

    var fc_push_sort = restfulParams.fc_push_sort;


    //任务流创建
    async.waterfall([
            //1.查询论坛分类
            function (cb) {
                // resultData.banner = [{
                //     titleImage: "http://testfileupload.oss-cn-beijing.aliyuncs.com/1528274840000-6miSaA.jpg",
                //     activityUrl: "http://localhost:8888/#/organization_offline_activity?oa_id=355"
                // }];
                resultData.banner = [];
                cb();
            },
            function (cb) {
                if (fc_push_sort == 1) {
                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchQuestionFixUser + "/sort_order/fc_push_sort-desc/skip/" + skip + "/limit/" + limit,
                            method: 'POST',
                            data: {
                                fields: ['fc_id', 'fc_title', 'fc_content', 'fc_images', 'fc_create_time', 'u_id', 'u_type', 'u_oid', 'u_nickname', 'u_header_url', 'fc_sort', 'fc_push_sort'].join(",")
                            }
                        },
                        function (err, data) {

                            console.log(111,data);

                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.questionData = data.data;

                            cb();
                        }
                    );
                } else if (fc_sort == 1) {

                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.searchQuestionFixUser + "/sort_order/fc_sort-fc_create_time-desc/skip/" + skip + "/limit/" + limit,
                            method: 'POST',
                            data: {
                                fields: ['fc_id', 'fc_title', 'fc_content', 'fc_images', 'fc_create_time', 'u_id', 'u_type', 'u_oid', 'u_nickname', 'u_header_url', 'fc_sort', 'fc_push_sort'].join(",")
                            }
                        },
                        function (err, data) {

                            //判断是否有错误信息
                            if (err) {
                                return cb(err, data);
                            }

                            resultData.questionData = data.data;

                            cb();
                        }
                    );
                }

            },
            function (cb) {

                var questionData = resultData.questionData;

                var tasks = [];

                questionData.forEach(function (item, i) {

                    tasks.push(function (cb) {

                        R.SEND_HTTP(
                            req,
                            {
                                url: CORE_APIConfig.coreAPI.searchContentAnswer + "/fcc_fcid/" + item.fc_id,
                                method: 'POST',
                                data: {
                                    fields: ["fca_zan_count", "fca_comment_count"].join(",")
                                }
                            },
                            function (err, data) {

                                //判断是否有错误信息
                                if (err) {
                                    return cb(err, data);
                                }

                                var answerData = data.data;

                                if (answerData.length > 0) {

                                    var zanCount = 0;

                                    var commentCount = 0;

                                    for (var i = 0; i < answerData.length; i++) {

                                        zanCount += parseInt(answerData[i].fca_zan_count);

                                        commentCount += parseInt(answerData[i].fca_comment_count)

                                    }

                                    item.zanCount = zanCount;

                                    item.commentCount = commentCount;

                                } else {

                                    item.zanCount = 0;

                                    item.commentCount = 0;

                                }

                                cb();
                            }
                        );
                    })
                });
                async.waterfall(tasks, cb);
            },
            function (cb) {

                var questionData = resultData.questionData;

                var tasks = [];

                questionData.forEach(function (item, i) {

                    tasks.push(function (cb) {
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.searchOrganization + "/o_uid/" + item.u_id,
                                    method: 'POST',
                                    data: {
                                        fields: ["o_name", "o_logo"].join(",")
                                    }
                                },
                                function (err, data) {

                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    var organizationData = data.data[0];

                                    if(data.data.length>0){

                                        item.isOrganization = 1;

                                        item.o_name = organizationData.o_name;

                                        item.o_logo = organizationData.o_logo;

                                        cb();

                                    }else{

                                        item.isOrganization = 0;

                                        cb();
                                    }

                                }
                            );

                    })
                });
                async.waterfall(tasks, cb);
            },

        ], function (err, result) {

            //返回最终拼接结果
            RES.response(res, true, resultData);

        }
    );


}